<template>
    <div class="common">
        <innerCommon 
            :location="location"
        >
            <template slot="left">
                <span>{{'活动'+(type==0?'新增':'修改')}}</span>
            </template>
            <div slot="right">
                <a-button type="primary" @click="returnBack()">返回</a-button>
            </div>
            <div slot="table">
                <eform
                    v-if="fields||type==0"
                    :formData="formData"
                    class="form"
                    ref="form"
                    :fields="fields"
                >
                    <div slot="time">
                        <rangeTime
                            v-decorator="['time',{rules: [{ required: true, message: '请选择活动时间' }]}]"
                            @getTimes="getTimes"
                            :defaultTime="defaultTime"
                        ></rangeTime>
                    </div>
                    <div slot="content">
                        <eQuill
                            @getQuillContent="getQuillContent"
                            v-decorator="['content',{rules: [{ required: true, message: '请输入活动内容' }]}]"
                            :defaultData="defaultData"
                        ></eQuill>
                    </div>
                    <div slot="attachFile">
                        <a-tag v-for="(item,index) in fileList" :key="index">
                            {{item.filename}}<a-icon type="close" @click="deleteFile(index)" />
                        </a-tag>
                        <uploadFile 
                            name="上传附件"
                            @returnFileUrl="returnFileUrl"
                        ></uploadFile>
                    </div>
                </eform>
                <div class="submitBtn">
                    <a-button @click="returnBack()">取消</a-button>
                    <a-button type="primary" @click="save()">保存</a-button>
                </div>
            </div>
        </innerCommon>
    </div>
</template>
<script>
import eQuill from "@/components/common/eQuill"
import rangeTime from "@/components/common/rangeTime"
import innerCommon from "@/components/common/innerCommon"
import eform from "@/components/common/form"
import moment from "moment"
import uploadFile from "@/components/common/uploadFile"
import {TrainingManagerCommitApi,editTrainingManagerCommitApi} from "@/request/api"
export default {
    name:'trainingManagerAdd',
    components:{innerCommon,eform,rangeTime,eQuill,uploadFile},
    created(){
        if(this.$route.query.data){
            let info=JSON.parse(this.$route.query.data)
            this.type=1
            this.trainingId=info.id
            this.fields=info
            this.defaultData=info.content
            this.fields.time=this.defaultTime={'start':info.startTime,'end':info.endTime}
            this.fileList=info.attachFile||[]
        }else{
            this.type=0
        }
        this.location=[{
            name:"教学课堂管理",
            path:''
        },{
            name:"培训管理",
            path:'/trainingManager'
        },{
            name:this.type==0?"新增":"修改",
            path:'/trainingManagerAdd'
        }]
    },
    data(){
        const formData=[
            {
                label:"活动名称",
                prop:"title",
                placeholder:'请输入标题',
                rules:[{required:true,message:'请输入标题'}],
            },{
                label:"活动时间",
                scopedSlots: { customRender: 'time'},
            },{
                label:"活动内容",
                scopedSlots: { customRender: 'content'},
            },{
                label:"附件",
                scopedSlots: { customRender: 'attachFile'},
            }
        ]
        return{
            formData,
            location:[],
            type:0, //0是新增 1是修改
            fields:null,
            defaultData:'',
            quillInfo:'',
            trainingId:'',
            defaultTime:null,
            fileList:[]
        }
    },
    methods:{
        moment,
        returnBack(){
            this.$router.go(-1)
        },
        getQuillContent(data){
            this.quillInfo=data
            this.$refs.form.form.setFieldsValue({['content']:data})
        },
        getTimes(start,end){
            let time={start,end}
            this.$refs.form.form.setFieldsValue({['time']:time})
        },
        returnFileUrl(data){
            data.data.docUrl.forEach(item=>{
                this.fileList.push(item)
            })
        },
        deleteFile(data){
            this.fileList.splice(data,1)
        },
        save(){
            this.$refs.form.form.validateFields().then(res=>{
                if(moment(res.time.start).unix()>moment(res.time.end).unix()){
                    this.$message.error('结束时间不能小于开始时间')
                    return
                }
                let formData={
                    ...res,
                    startTime:moment(res.time.start).format('YYYY-MM-DD HH:mm:ss'),
                    endTime:moment(res.time.end).format('YYYY-MM-DD HH:mm:ss'),
                    'attachFile':this.fileList.length==0?null:this.fileList
                }
                let api
                if(this.type==0){
                    api=TrainingManagerCommitApi
                }else{
                    api=editTrainingManagerCommitApi
                    formData={...formData,'classId':this.trainingId}
                }
                this.getToken(api,formData,res=>{
                    this.$message.success(res.message)
                    this.returnBack()
                })
            })
        }
    }
}
</script>
<style lang="stylus" scoped>

</style>